package com.yomahub.liteflow.example.xstream.converter;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.yomahub.liteflow.example.xstream.Person;

/**
 * @program: liteflow-example
 * @description:
 * @author: yyyyjinying
 * @create: 2023-09-25 23:27
 **/
public class PersonConverter implements Converter {
    @Override
    public boolean canConvert(Class clazz) {
        return clazz.equals(Person.class);
    }
    @Override
    public void marshal(Object value, HierarchicalStreamWriter writer,
                        MarshallingContext context) {
        Person person = (Person) value;
        writer.startNode("fullname");
        writer.setValue(person.getName());
        writer.endNode();
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader,
                            UnmarshallingContext context) {
        Person person = new Person();
        reader.moveDown();
        person.setName(reader.getValue());
        reader.moveUp();
        return person;
    }
}
